package com.felix.ocoupon.entity;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.felix.ocoupon.constant.DistributeTarget;
import com.felix.ocoupon.constant.OcouponCategory;
import com.felix.ocoupon.constant.ProductLine;
import com.felix.ocoupon.converter.DistributeTargetConverter;
import com.felix.ocoupon.converter.OcouponCategoryConverter;
import com.felix.ocoupon.converter.ProductLineConverter;
import com.felix.ocoupon.converter.RuleConverter;
import com.felix.ocoupon.serialization.OcouponTemplateSerialize;
import com.felix.ocoupon.vo.TemplateRule;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author hsufelix
 * @email hsufelix@yeah.net
 * @Date 2020/11/11
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name="ocoupon_template")
@JsonSerialize(using = OcouponTemplateSerialize.class)
public class OcouponTemplate implements Serializable {

    // 自增主键
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private  Integer id;
    // 是否可用
    @Column(name = "available", nullable = false)
    private Boolean available;
    // 是否过期
    @Column(name = "expired", nullable = false)
    private Boolean expired;
    // 名称
    @Column(name = "name", nullable = false)
    private String name;
    // logo
    @Column(name = "logo", nullable = false)
    private String logo;
    // 描述
    @Column(name = "description", nullable = false)
    private String description;
    // 优惠券种类
    @Column(name = "category", nullable = false)
    @Convert(converter = OcouponCategoryConverter.class)
    private OcouponCategory category;
    // 产品线
    @Column(name = "productline", nullable = false)
    @Convert(converter = ProductLineConverter.class)
    private ProductLine productLine;

    // 总数
    @Column(name = "counts", nullable = false)
    private Integer counts;

    // 创建时间
    @CreatedDate
    @Column(name = "createtime", nullable = false)
    private Date createTime;

    // 用户ID
    @Column(name = "userid", nullable = false)
    private Long userId;

    // 优惠券模板的编码
    @Column(name = "key", nullable = false)
    private String key;

    // 目标用户
    @Column(name = "target", nullable = false)
    @Convert(converter = DistributeTargetConverter.class)
    private DistributeTarget target;

    // 优惠券规则
    @Column(name = "rule", nullable = false)
    @Convert(converter = RuleConverter.class)
    private TemplateRule rule;

    // 自定义构造函数
    public OcouponTemplate(String name, String logo,
                           String description, String category, Integer productLine,
                           Integer counts, Long userId,
                           Integer target, TemplateRule rule) {
        this.name = name;
        this.logo = logo;
        this.description = description;
        this.category = OcouponCategory.of(category);
        this.productLine = ProductLine.of(productLine);
        this.counts = counts;
        this.userId = userId;
        // 优惠券唯一编码 = 4（产品线和类型） + 8(日期：20201112) + id（扩充为4位）
        this.key = productLine.toString() + category + new SimpleDateFormat("yyyyMMdd").format(new Date());
        this.target = DistributeTarget.of(target);
        this.rule = rule;
    }
}
